package gamesys.games.particlesystem.core {
	import gamesys.games.particlesystem.AbstractTest;

	import particlesystem.core.ParticleCanvas;
	import particlesystem.core.ParticleEffect;
	import particlesystem.core.ParticleSettings;
	import particlesystem.effects.DecayEffect;
	import particlesystem.effects.GravityEffect;
	import particlesystem.effects.LifeEffect;
	import particlesystem.effects.VelocityEffect;
	import particlesystem.graphics.BitmapAnimationFactory;
	import particlesystem.utils.Range;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldType;

	/**
	 * @author robertthurston
	 */
	[SWF (width="577", height="469", frameRate="30", backgroundColor="0x222222")]

	public class ParticleEffectTest2 extends AbstractTest {

		private var particleEffect : ParticleEffect;
		private var particleCanvas : ParticleCanvas;
		private var particleSettings : ParticleSettings;
		private var emitter : Sprite;
		private var bitmapAnimationFactory : BitmapAnimationFactory;
		private var minGravity : TextField;
		private var maxGravity : TextField;

		override protected function init() : void {
			setupEmitter();
			createInterface();
			
			particleCanvas = new ParticleCanvas(577, 469);
			bitmapAnimationFactory = new BitmapAnimationFactory();
			particleSettings = new ParticleSettings();
			particleEffect = new ParticleEffect(particleSettings, particleCanvas);
			updateSettings();
			addChild(particleCanvas);
			
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}

		private function updateSettings() : void {
			particleSettings.startBounds = new Rectangle(1, 1, 2, 2);
			particleSettings.startDelayRange = new Range(0, 0);
			particleSettings.reSpawn = false;
			particleSettings.emitter = emitter;
			particleSettings.bitmapAnimationParticles = [bitmapAnimationFactory.getBitmapAnimation("FairyDust1")];
		}

		private function updateEffect() : void {
			trace(minGravity.text);
			trace(maxGravity.text);
			
			particleEffect.removeEffects();
			particleEffect.addEffect(new LifeEffect(10, 10, 10));
			particleEffect.addEffect(new VelocityEffect(1, 20, 0, 360));
			particleEffect.addEffect(new GravityEffect(Number(minGravity.text), Number(maxGravity.text)));
			particleEffect.addEffect(new DecayEffect(.90, .95));
		}

		private function setupEmitter() : void {
			emitter = new Sprite();
			emitter.graphics.beginFill(0xFF0000);
			emitter.graphics.drawCircle(5, 5, 5);
			emitter.x = 275;
			emitter.y = 200;
			addChild(emitter);
		}

		private function createButton(xPos : Number, yPos : Number, listener : Function) : Sprite {
			var button : Sprite = new Sprite();
			button.graphics.beginFill(0xFF0000);
			button.graphics.drawRect(0, 0, 60, 20);
			button.addEventListener(MouseEvent.CLICK, listener);
			button.x = xPos;
			button.y = yPos;
			addChild(button);
			
			return button;
		}

		private function createTextField(xPos : Number, yPos : Number, title : String) : TextField {
			var header : TextField = new TextField();
			header.x = xPos;
			header.y = yPos;
			header.text = title;
			header.width = 50;
			header.height = 30;
			header.background = true;;
			header.border = true;
			
			var textField : TextField = new TextField();
			textField.x = xPos;
			textField.y = yPos + 20;
			textField.background = true;;
			textField.border = true;
			textField.width = 50;
			textField.height = 30;
			textField.type = TextFieldType.INPUT;
			textField.name = title;
			textField.text = "0";
			
			addChild(header);
			addChild(textField);
			
			return textField;
		}

		private function createInterface() : void {
			minGravity = createTextField(0, 20, "minGravity");
			maxGravity = createTextField(50, 20, "maxGravity");
			var minSpeed : TextField = createTextField(100, 20, "minSpeed");
			var maxSpeed : TextField = createTextField(150, 20, "maxSpeed");
			var applyButton : Sprite = createButton(0, 100, apply);
		}

		private function apply(event : MouseEvent) : void {
			updateEffect();
			updateSettings();
		}

		private function onDown(event : MouseEvent) : void {
			particleEffect.addParticles(1000);
		}

		private function onEnterFrame(event : Event) : void {			
			emitter.x = mouseX;
			emitter.y = mouseY;
			particleCanvas.clear();
			particleEffect.update();
		}
	}
}
